package bid.yanhj.springboot.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传
 * 
 * @author yanhaijiang
 *
 */
// @Controller
@RestController // 所有类下的方法做json格式化转换
public class FileUploadController {

    @RequestMapping("/fileUpload")
    public Map<String, Object> fileUpload(@RequestParam("fileName") MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
//        if (file.isEmpty()) {
//            map.put("msg", "上传失败，请选择文件");
//            return map;
//        }
        String fileName = file.getOriginalFilename();
        String filePath = "/Users/yanhaijiang/baidu/";
        File dest = new File(filePath + fileName);
        System.out.println(dest);
        try {
            file.transferTo(dest);
            map.put("msg", "上传成功");
//            LOGGER.info("上传成功");
            System.out.println("成功");
            return map;
        } catch (IOException e) {
//            LOGGER.error(e.toString(), e);
            System.out.println("失败");
            map.put("msg", "失败");
        }
        return map;
    }
}
